import {Init,InitDataCenter} from './3DInit.js'
import {ModelLoader} from './ModelLoader.js'
import {Load} from "./Load.js";
import {Param} from "./Param.js";
import {BaseInfo} from "./ModelInfoWindow.js";
import {MouseEvent,BaseMouseEvent} from "./MouseEvent.js"

export let scene = Param.scene;
export let camera = Param.camera;
export let renderer = Param.renderer;
let initDataCenter = new InitDataCenter(Param);
initDataCenter.initAll();
LoadExample();
initDataCenter.addContextEventListener('click', MouseClick, false);

/*function MouseClick() {
    let s
    onClickMouse(event, camera, scene, function (selected) {
        let object = selected.object;
        if(object.name =='SWITCH_1_HUAWEI_CE6851'){
            let baseInfo = new BaseInfo()
            baseInfo.displayWindow()
        }
    })
}*/

async function MouseClick() {
    let targetObject= await BaseMouseEvent.getClickObject(event,camera,scene);
    let object = targetObject.object;
    if(object.name =='SWITCH_1_HUAWEI_CE6851'){
        let baseEvent = new BaseMouseEvent();
        baseEvent.clickEvent();
    }
}


/*async function LoadModel(){
    let material= await ModelLoader.createMTLLoader("./models/","SWITCH_1_HUAWEI_CE6851.mtl");
    let obj = await ModelLoader.createOBJLoader("./models/","SWITCH_1_HUAWEI_CE6851.obj",material)
    obj.scale.set(0.04,0.04,0.04);
    obj.position.x=(parseInt(parseInt(counter/40)%10))*2.5;
    obj.position.y=(counter%40)*0.2;
    obj.position.z=(counter/40/10)*6;
    scene.add(obj);
    counter++
}*/

/*async function LoadModelClone(arr) {
    console.time("开始加载模型");
    let material= await ModelLoader.createMTLLoader("./models/","SWITCH_1_HUAWEI_CE6851.mtl");
    let obj = await ModelLoader.createOBJLoader("./models/","SWITCH_1_HUAWEI_CE6851.obj",material)
    obj.scale.set(0.04,0.04,0.04);
    if(arr.length>1){
        for(let i=1;i < arr.length;i++){
            let cabClone = obj.clone();
            cabClone.position.set(arr[i][0],arr[i][1],arr[i][2]);
            cabClone.traverse(function(child) {
                if (child instanceof THREE.Mesh) {
                    if(child.material instanceof Array){
                        for(let i=0;i<child.material.length;++i){
                            child.material[i].side =THREE.DoubleSide ;
                            child.material[i]=child.material[i].clone() ;
                        }
                    } else{
                        child.material=child.material.clone();
                        child.material.side =THREE.DoubleSide;
                    }
                }
            });
            scene.add( cabClone );
        }
    }
    console.timeEnd("开始加载模型")
}*/

/*async function babylonLoad() {
    let obj = await ModelLoader.createBabylonLoader("./models/babylon/", "skull.babylon");
    obj.traverse((item) => {
        if (item.isMesh) {
            item.material = new MeshPhongMaterial({
                color: 0xffffff * Math.random()
            })
        }
    });
    // 重新设置对象位置
    obj.children.forEach(item => {
        if (item.type === 'Mesh') {
            item.position.set(0, 0, 0);
        }
    });
    scene.add(obj);
}*/
/*function LoadMultiModels() {
    for(let i=0;i<10;i++){
        LoadModel()
    }
}*/

/*async function LoadModel1(){
    let loaderFactory = new LoaderFactory()
    let material=await loaderFactory.getLoader(loaderType.MTLLoader,"./models/","SWITCH_1_HUAWEI_CE6851.mtl");
    let obj = await loaderFactory.getLoader(loaderType.OBJLoader,"./models/","SWITCH_1_HUAWEI_CE6851.obj",material)
    obj.scale.set(0.2,0.2,0.2)
    obj.position.set(20,5,0)
    obj.rotation.set(-3,0,3)
    scene.add(obj)
}*/

/*
LoadModel()
LoadModel1()*/
//LoadMultiModels()
//babylonLoad()


/*initDataCenter.loadModel("SWITCH_1_HUAWEI_CE6851","./models/",function(object){
    object.scale.set(0.01,0.01,0.01);
});*/

function createArray(num){
    let arr=[];
    for(let i=0;i < num;i++){
        let x = (parseInt(parseInt(i/20)%10))*3;
        let y = (i%20)*1.7;
        let z = (i/20/10)*8;
        arr[i] =[x,y,z]
    }
    return arr
}

async function LoadExample(arr) {
    console.time("开始加载模型");
    let material= await ModelLoader.createMTLLoader("./models/","SWITCH_1_HUAWEI_CE6851.mtl");
    let obj = await ModelLoader.createOBJLoader("./models/","SWITCH_1_HUAWEI_CE6851.obj",material);
    scene.add(obj);
    obj.scale.set(0.2,0.2,0.2);
    //Load.loadCloneModel(arr,scene,obj);
    console.timeEnd("开始加载模型")
}